define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'account/index' + location.search,
                    add_url: 'account/add',
                    edit_url: 'account/edit',
                    del_url: 'account/del',
                    multi_url: 'account/multi',
                    import_url: 'account/import',
                    table: 'account',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showColumns: false,
                searchFormVisible: true,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false,sortable:true},
                        {field: 'acc_type', title: __('Acc_type'), searchList: {"1":__('Acc_type 1'),"2":__('Acc_type 2'),"3":__('Acc_type 3'),"4":__('Acc_type 4'),"0":__('Acc_type 0')}, formatter: Table.api.formatter.normal},
                        {field: 'shouli_date', title: __('Shouli_date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,sortable:true},
                        {field: 'zhanghao', title: __('Zhanghao'), operate: 'LIKE'},
                        {field: 'password', title: __('Password'), operate: 'LIKE',formatter:function(value, row, index){
                                var realPassword = value || '';
                                // 创建密码显示容器
                                var html = '<div class="password-cell" style="display: flex; align-items: center; gap: 8px;">';
                                html += '<span class="password-text" style="flex: 1; font-family: monospace; letter-spacing: 2px;" data-password="' + realPassword + '">******</span>';
                                html += '<button type="button" class="btn btn-xs btn-default toggle-password" title="显示/隐藏密码" data-index="' + index + '" style="padding: 2px 6px; border: none; background: transparent;">';
                                html += '<i class="fa fa-eye"></i>';
                                html += '</button>';
                                html += '</div>';
                                return html;
                            }},
                        {field: 'zhaoshu', title: __('Zhaoshu'), operate: 'LIKE'},
                        {field: 'stop_date', title: __('Stop_date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,sortable:true},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'card', title: __('Card'), operate: 'LIKE'},
                        {field: 'is_yajin', title: __('Is_yajin'), searchList: {"1":__('Is_yajin 1'),"0":__('Is_yajin 0')}, formatter: Table.api.formatter.normal},
                        {field: 'user_date', title: __('User_date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,sortable:true},
                        {field: 'guishu', title: __('Guishu'), operate: 'LIKE'},
                        {field: 'zidan', title: __('Zidan'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},

                        //{field: 'updatetime', title: __('Updatetime'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('操作'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('发送故障'),
                                    title: __('发送故障'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'accountsend/add',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: __('操作记录'),
                                    title: __('操作记录'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'accountlog/index?id={id}',
                                    callback: function (data) {

                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: '清空用户资料',
                                    text: __('清空用户资料'),
                                    title: __('清空用户资料'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'account/userclear?id={id}',
                                    refresh:true,
                                    confirm: '确认发送',
                                },

                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                // 在表格加载完成后绑定事件
                onPostBody: function() {
                    // 绑定密码显示/隐藏事件
                    $('.toggle-password').off('click').on('click', function() {
                        var $button = $(this);
                        var $icon = $button.find('i');
                        var $passwordText = $button.closest('.password-cell').find('.password-text');
                        var realPassword = $passwordText.data('password');

                        if ($passwordText.text() === '******') {
                            // 显示密码
                            $passwordText.text(realPassword);
                            $icon.removeClass('fa-eye').addClass('fa-eye-slash');
                            $button.attr('title', '隐藏密码');
                        } else {
                            // 隐藏密码
                            $passwordText.text('******');
                            $icon.removeClass('fa-eye-slash').addClass('fa-eye');
                            $button.attr('title', '显示密码');
                        }
                    });
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $('head').append('<style>' +
                '.password-cell { display: flex; align-items: center; gap: 8px; }' +
                '.password-text { flex: 1; font-family: monospace; letter-spacing: 2px; }' +
                '.toggle-password { transition: all 0.3s; }' +
                '.toggle-password:hover { color: #3498db; background-color: #f8f9fa !important; }' +
                '</style>');
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'account/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'account/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'account/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
